'use strict'

let templateConfig = require('../../config/templateConfig')
  let setSkillRateConfig = templateConfig.other[290033]

exports.setSkillBook = function(skills, skillInfo) {
  let result //-2 附魂失败 -1新增技能 其他替换的技能
    //判断技能是否是初级的
  if (skillInfo.skillLvl === 1) {
    if (skills.length === 0) {
      skills[0] = skillInfo.skillId
      result = -1
    } else if (skills.length === 4) {
      if (Math.random() * 100 < setSkillRateConfig.rate1[0]) {
        let index = Math.floor(Math.random() * 4)
        result = skills[index]
        skills[index] = skillInfo.skillId
      } else {
        result = -2
      }
    } else {
      if (Math.random() * 100 < setSkillRateConfig.rate1[0]) {
        if (Math.random() * 100 < 50) { //新增技能
          result = skills.length
          skills[skills.length] = skillInfo.skillId
          result = -1
        } else { //替换技能
          let index = Math.floor(Math.random() * skills.length)
          result = skills[index]
          skills[index] = skillInfo.skillId
        }
      } else {
        result = -2
      }
    }
  } else if (skillInfo.skillLvl === 2) { //高级技能
    //判断是否有前置技能
    let i = 0
    for (; i < skills.length; i++) {
      if (skills[i] === skillInfo.preSkill) {
        break
      }
    }
    if (i === skills.length) { //没有前置技能
      if (skills.length === 4) {
        if (Math.random() * 100 < setSkillRateConfig.rate2[0]) { //替换技能
          let index = Math.floor(Math.random() * 4)
          result = skills[index]
          skills[index] = skillInfo.skillId
        } else {
          result = -2
        }
      } else {
        if (Math.random() * 100 < setSkillRateConfig.rate2[0]) {
          if (Math.random() * 100 < 50) { //新增技能
            result = skills.length
            skills[skills.length] = skillInfo.skillId
            result = -1
          } else { //替换技能
            let index = Math.floor(Math.random() * skills.length)
            result = skills[index]
            skills[index] = skillInfo.skillId
          }
        } else {
          result = -2
        }
      }
    } else { //有前置技能
      if (Math.random() * 100 < setSkillRateConfig.rate2[1]) {
        result = skills[i]
        skills[i] = skillInfo.skillId
      } else {
        result = -2
      }
    }
  }
  return result
}
